package api

import (
	"context"
	"gitee.com/leo-xin/novel_distribution_sdk_go/yuewen/config"
	"net/http"
	"net/url"
)

type BookGetFreeChapterListApiService service

type BookGetFreeChapterListRequest struct {
	ctx        context.Context
	ApiService *BookGetFreeChapterListApiService
	bookId     *uint64
	appFlag    *string
}

type BookGetFreeChapterListResponse struct {
	Code    *int64           `json:"code,omitempty"`
	Message *string          `json:"message,omitempty"`
	Data    *ChapterListData `json:"data,omitempty"`
}

type ChapterListData struct {
	BookId      *string        `json:"book_id,omitempty"`
	ChapterList []*ChapterInfo `json:"chapter_list,omitempty"`
}

type ChapterInfo struct {
	ChapterId   *string `json:"chapter_id,omitempty"`
	ChapterSeq  *uint   `json:"chapter_seq,omitempty"`
	ChapterName *string `json:"chapter_name,omitempty"`
}

func (r *BookGetFreeChapterListRequest) BookId(bookId uint64) *BookGetFreeChapterListRequest {
	r.bookId = &bookId
	return r
}

func (r *BookGetFreeChapterListRequest) AppFlag(appFlag string) *BookGetFreeChapterListRequest {
	r.appFlag = &appFlag
	return r
}

func (r *BookGetFreeChapterListRequest) Execute() (*BookGetFreeChapterListResponse, *http.Response, error) {
	return r.ApiService.getExecute(r)
}

func (r *BookGetFreeChapterListRequest) ApiSecret(ApiSecret string) *BookGetFreeChapterListRequest {
	r.ctx = context.WithValue(r.ctx, config.ApiSecret, ApiSecret)
	return r
}

func (r *BookGetFreeChapterListRequest) Email(Email string) *BookGetFreeChapterListRequest {
	r.ctx = context.WithValue(r.ctx, config.Email, Email)
	return r
}

func (r *BookGetFreeChapterListRequest) WithLog(enable bool) *BookGetFreeChapterListRequest {
	if enable {
		r.ctx = context.WithValue(r.ctx, config.ContextEnableLog, true)
	}
	return r
}

func (a *BookGetFreeChapterListApiService) Get(ctx context.Context) *BookGetFreeChapterListRequest {
	return &BookGetFreeChapterListRequest{
		ApiService: a,
		ctx:        ctx,
	}
}

func (a *BookGetFreeChapterListApiService) getExecute(r *BookGetFreeChapterListRequest) (*BookGetFreeChapterListResponse, *http.Response, error) {

	cfg := a.client.Cfg
	var (
		localVarPath         = cfg.GetBasePath() + "/" + cfg.Prefix + "/Book/GetFreeChapterList"
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     map[string]string
		localVarFormParams   = url.Values{}
		localVarHeaderParams = make(map[string]string)
		localVarQueryParams  = url.Values{}
		localVarReturnValue  *BookGetFreeChapterListResponse
	)

	r.ctx = a.client.prepareCtx(r.ctx)

	if ok, GenericOpenAPIError := hasAppFlag(r.appFlag); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}

	if ok, GenericOpenAPIError := hasReqParam(r.bookId, "BookId"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}

	localVarQueryParams.Add("book_id", toString(r.bookId))
	localVarQueryParams.Add("appflag", *r.appFlag)

	req, err := a.client.buildRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.call(r.ctx, req, &localVarReturnValue)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}
	return localVarReturnValue, localVarHTTPResponse, nil
}
